// LGUITabstrip.cpp: implementation of the LGUITabstrip class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LAMEBuffer.h"
#include "LGUI.h"
#include "LAMEFont.h"
#include "LGUITabstrip.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

LGUITabstrip::LGUITabstrip(int _x, int _y, int _w, int _h) : LGUIWindow(_x,_y,_w,_h)
{
	curTab = 0;
	numTabs = 0;
}

LGUITabstrip::~LGUITabstrip()
{
}


void LGUITabstrip::addTab(const TCHAR *text, LGUIPanel *panel)
{
	if (numTabs>=8) return;
	LGUITab &t = tabs[numTabs];
	t.panel=panel;
	t.text = text;
	t.width = fonts[0].textSize(text)+5;
	numTabs++;
}


void LGUITabstrip::draw(int xoff, int yoff)
{
	if (!isDirty) return;
	buffer.hLine(xoff+x,yoff+y,xoff+x+w,0);
	int tx=xoff+x+5;
	buffer.rectFill(x+xoff,yoff+y+1,x+xoff+w,y+yoff+h, COLOR(0xFFFFFF));

	for (int i=0; i<numTabs; i++)
	{
		LGUITab &t = tabs[i];
		int bgcol = i == curTab ? COLOR(0xb0b0b0) : COLOR(0x707070);
		int off=i==curTab ? 2 : 0;
		if (tabs[i].panel)
		{
			//bgcol = i == curTab ? tabs[i].panel->getColor() : darkenColor(tabs[i].panel->getColor());
			bgcol = t.panel->getColor();
		}

		buffer.rectFill(tx+1-off,yoff+y, tx+t.width-1+off,yoff+y+h-1,bgcol);
		buffer.rect(tx+1-off,yoff+y, tx+t.width-1+off,yoff+y+h-1,0);

		if (i==curTab)
			buffer.hLine(tx,yoff+y,tx+t.width,bgcol);

		buffer.putPixel(tx+1-off,yoff+y+h-1,0xffff);
		buffer.putPixel(tx+t.width-1+off,yoff+y+h-1,0xffff);

		buffer.drawText(tabs[i].text,tx+3,yoff+y+1+(i==curTab ? 0 : 1),0xffff);
		tx += t.width;
	}
	buffer.rectFillDarken(x+xoff,yoff+y+1,x+xoff+w,yoff+y+4);
	tx=xoff+x+5;
	for (int i=0; i<numTabs; i++)
	{
		LGUITab &t = tabs[i];
		if (i==curTab)
		{
			int bgcol = COLOR(0x707070);
			if (tabs[i].panel)
				bgcol = darkenColor(t.panel->getColor());
			buffer.rectFill(tx,yoff+y, tx+t.width+1,yoff+y+h-1,bgcol);
			buffer.drawTextShadow(tabs[i].text,tx+3,yoff+y+1,0xffc0,0);
		}
		tx += t.width;
	}  
	buffer.addDirtyRect(*this);
	isDirty = false;
}


void LGUITabstrip::penDown(int px, int py)
{

	if (py<=y || py>y+h) return;
	int tx=x+5;
	for (int i=0; i<numTabs; i++)
	{
		LGUITab &t = tabs[i];
		if (px>=tx && px<tx+t.width)
		{
			if (curTab != i)
			{
				if (tabs[curTab].panel)
					tabs[curTab].panel->hide(false);

				curTab=i;
				if (tabs[curTab].panel)
					tabs[curTab].panel->show();
				redraw();
			}
			return;
		}
		tx+=t.width;
	}
}